package edu.ou.herbarium.threads2;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.nio.channels.FileChannel;

import edu.ou.herbarium.sharedata.ErrorData;
import edu.ou.herbarium.sharedata.ShareData5;
import edu.ou.herbarium.sharedata.ShareData6;
import edu.ou.herbarium.sharedata.TransferData5;
import edu.ou.herbarium.sharedata.TransferData6;
import edu.ou.herbarium.util.ASyncUDPSvr;

public class ImageThread extends Thread{
	ShareData5 s5;
	ShareData6 s6;
	ErrorData de;
	
	int index = 0;
	
	public ImageThread(ShareData5 s5,ShareData6 s6,ErrorData de){
		this.s5 = s5;
		this.s6 = s6;
		this.de = de;
	}
	
	public void run(){
		try {
		String sentence = "";
		DatagramSocket serverSocket = new DatagramSocket(60005);
		while (true) {
			TransferData5 d5 = s5.getShareData();
				boolean isCameraOk = true;
				
				
				
				
				
				
//				do{
				String host = "localhost";
				int port = 60002;
				byte[] message = d5.getOriginalFileName().getBytes();
				InetAddress address = InetAddress.getByName(host);
				DatagramPacket packet = new DatagramPacket(message,
						message.length, address, port);
				DatagramSocket dsocket = new DatagramSocket();
				dsocket.send(packet);
			
				
				byte[] receiveData = new byte[1024];
				for (int i = 0; i < receiveData.length; i++)
					receiveData[i] = 0;
				DatagramPacket receivePacket = new DatagramPacket(receiveData,
					receiveData.length);
				serverSocket.receive(receivePacket);
				sentence = new String(receivePacket.getData());
				if(sentence.startsWith("ERROR"))
					isCameraOk = false;
//				}while(isCameraOk==false);
				
				
				//Camera operation finished
				copyFile(sentence,d5.getOriginalFileName()+"full.jpg");
				
				TransferData6 d6 = new TransferData6(d5.getSpec(),d5.getS2(),d5.getOriginalFileName(),"");
				this.s6.setShareData(d6);
				//stub
//				String name = ((index%100)+1)+".jpg";
//				System.out.println(name);
//				copyFile("C:/Users/eming/Desktop/image/"+name,d5.getOriginalFileName()+"full.jpg");
//				
//				TransferData6 d6 = new TransferData6(d5.getSpec(),d5.getOriginalFileName(),"");
//				this.s6.setShareData(d6);
//				index++;
			
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public long copyFile(String srcFileName, String newFileName) {  
		File srcFile = new File(srcFileName);
        long copySizes = 0;  
        if (!srcFile.exists()) {  
            copySizes = -1;  
        } else if (newFileName == null) {  
            copySizes = -1;  
        } else {  
            try {  
                FileChannel fcin = new FileInputStream(srcFile).getChannel();  
                FileChannel fcout = new FileOutputStream(new File(newFileName)).getChannel();  
                long size = fcin.size();  
                fcin.transferTo(0, fcin.size(), fcout);  
                fcin.close();  
                fcout.close();  
                copySizes = size;  
            } catch (FileNotFoundException e) {  
                e.printStackTrace();  
            } catch (IOException e) {  
                e.printStackTrace();  
            }  
        }  
        return copySizes;  
    }  
}
